#!/bin/bash


cd $(dirname $(readlink -f $0));

merge_list_file=$1
cmd="game"

if [ -z $merge_list_file ]; then
    merge_list_file=./merge/merge_list.txt
elif [ "$merge_list_file" = "check" ]; then
    cmd="check"
    merge_list_file=./merge/merge_list.txt
fi



# read merge list to array
merge_list=()
while read line; do
    merge_list[${#merge_list[*]}]=$line
done < $merge_list_file

merge_count=${#merge_list[*]}
for ((i=0; i<merge_count; ++i)); do
    servers=${merge_list[$i]}

    if [ "$cmd" = "check" ]; then
        ./util/notice.sh "check" "$servers"
        ./merge/check.sh $servers
        if (($? != 0)); then
            exit 1
        fi
        continue;
    fi

    ./util/notice.sh "servers $servers" "process..."

    ./util/notice.sh "servers $servers" "backup db ..."
    ./merge/backup_db.sh $servers
    if (($? != 0)); then
        exit 1
    fi

    echo
    ./util/notice.sh "servers $servers" "merge db ..."
    ./merge/merge_db.sh $servers
    if (($? != 0)); then
        exit 1
    fi

    echo
    ./util/notice.sh "servers $servers" "update ..."
    ./merge/update.sh $servers
    if (($? != 0)); then
        exit 1
    fi
done

./util/notice.sh "merge" "DONE"
echo "决战天下重新分区并重置数据"
echo "王者战场重新分区并重置数据"
echo "跨服擂台重新分区并重置数据"
